package com.khshop.controller;


import com.baomidou.mybatisplus.core.metadata.IPage;
import com.khshop.pojo.Type;
import com.khshop.service.TypeService;
import com.khshop.vo.AjaxResult;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import jdk.nashorn.internal.ir.RuntimeNode;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.validation.Valid;
import java.io.File;
import java.util.Calendar;
import java.util.List;
import java.util.Map;
import java.util.UUID;

/**
 * <p>
 * 商品分类 前端控制器
 * </p>
 *
 * @author zhaoyang
 * @since 2020-10-13
 */
@Api(tags = "商品分类 前端控制器")
@Controller
@RequestMapping("/")
public class TypeController {

    @Autowired
    private TypeService typeService;

    @ApiOperation("条件分页查询商品分类")
    @PostMapping("/searchType")
    @ResponseBody
    public AjaxResult searchType(Map<String,Object> map, @RequestParam(value = "page",defaultValue = "1") Integer page, @RequestParam(value = "limit",defaultValue = "15") Integer limit){
        IPage<Type> typeIPage = typeService.search(map, page, limit);
        if (typeIPage != null) {
            return AjaxResult.success((int) typeIPage.getTotal(), typeIPage.getRecords());
        } else {
            return AjaxResult.error();
        }
    }

    @ApiOperation("增加商品分类")
    @PostMapping("/addType")
    @ResponseBody
    public AjaxResult addType(@Valid Type type, @RequestPart(value = "iconPhoto",required = false) MultipartFile multipartFile){
        System.err.println("新增的商品分类:"+type+"\n上传文件信息："+multipartFile);
        if (!multipartFile.isEmpty()) {
            //获得SpringBoot当前项目的路径：System.getProperty("user.dir")
            String path = System.getProperty("user.dir")+"/images/icon/";
            //按照月份进行分类：
            Calendar instance = Calendar.getInstance();
            String month = (instance.get(Calendar.MONTH) + 1)+"Month";
            path = path+month;

            System.err.println("获得的文件夹路径:" + path);
            String fName = multipartFile.getOriginalFilename();
            String houZhui = fName.substring(fName.lastIndexOf("."));
            String fileName = UUID.randomUUID() + houZhui;
            File file = new File(path, fileName);
            System.err.println("路径：" + path + ",名称：" + fileName);
            if (!file.exists()) {
                file.mkdirs();     //判断这个文件是否存在
            }
            type.setIcon(path+"/"+fileName);
            try {
                multipartFile.transferTo(file);// 将上传文件保存到相应位置
            } catch (Exception e) {
                e.printStackTrace();

            }

        }


        if (typeService.add(type)) {
            return  AjaxResult.success("增加成功");
        } else {
            return  AjaxResult.error("增加失败，请稍后重试！");
        }

    }

    @ApiOperation("更新商品分类")
    @PostMapping("/updateType")
    @ResponseBody
    public AjaxResult updateType(@RequestBody Type type){
        if (typeService.update(type)) {
            return  AjaxResult.success("修改成功");
        } else {
            return  AjaxResult.error("修改失败，请稍后重试！");
        }
    }


    @ApiOperation("根据父分类编号分页查看分类列表")
    @PostMapping("/findTypes")
    @ResponseBody
    public AjaxResult findTypes(int parentid){
        List<Type> types = typeService.findTypes(parentid);
        if (types.size()>0) {
            return  AjaxResult.success("查询成功",types);
        } else {
            return  AjaxResult.error("查询失败，请稍后重试！");
        }
    }

    @ApiOperation("批量转移分类商品")
    @PostMapping("/shiftMoveType")
    @ResponseBody
    public AjaxResult shiftMoveType(Long []oldTypeId, Long parentId){
        if (typeService.shiftMove(oldTypeId,parentId)) {
            return  AjaxResult.success("修改成功");
        } else {
            return  AjaxResult.error("修改失败，请稍后重试！");
        }
    }

    @ApiOperation("批量删除分类")
    @PostMapping("/delTypes")
    @ResponseBody
    public AjaxResult delTypes(Long []typeId){
        if (typeService.delTypes(typeId)) {
            return  AjaxResult.success("删除成功");
        } else {
            return  AjaxResult.error("删除失败，请稍后重试！");
        }
    }

}

